from nuscenes.nuscenes import NuScenes

# nuscenes 存放路径
nuscenes_data = "E:\\data\\3DpointCloud\\nuscenes\\nuscenes_mini"
nusc = NuScenes(version="v1.0-mini", dataroot=nuscenes_data, verbose=True)

# 1 nusenes basic 基础

# 如何找到 一个实例（instance）的所有 sample_annotation

one_instance = nusc.get('instance', nusc.sample_annotation[0]['instance_token'])
print(one_instance)

# 方法1 使用nusc.field2token()
ann_tokens = nusc.field2token("sample_annotation", "instance_token", one_instance["token"])

ann_tokens_field2token = set(ann_tokens)
print(ann_tokens_field2token)

# 方法2 
ann_record = nusc.get("sample_annotation", one_instance["first_annotation_token"])

ann_tokens_traverse = set()
ann_tokens_traverse.add(ann_record["token"])

while not ann_record["next"] == "":
    ann_record = nusc.get("sample_annotation", ann_record["next"])
    ann_tokens_traverse.add(ann_record["token"])

print(ann_tokens_field2token == ann_tokens_traverse)

# 反向索引 和 快捷字段
# nuscences数据集采用标准化数据存储，每个字段信息只存储一次，需要多次进行跨表查询
# 为提高效率，可以使用反向索引和 快捷字段 方法 (简而言之：类似于在数据库表中多添加一个字段)

# 1 使用shortcuts 
catname = nusc.sample_annotation[0]['category_name']
print(catname)

# 2 不使用shortcuts
ann_rec = nusc.sample_annotation[0]
inst_rec = nusc.get("instance", ann_rec["instance_token"])
cat_rec = nusc.get("category", inst_rec["category_token"])
print(catname == cat_rec["name"])

# 数据可视化

my_sample = nusc.sample[10]
nusc.render_pointcloud_in_image(my_sample["token"], pointsensor_channel="LIDAR_TOP")

nusc.render_pointcloud_in_image(my_sample['token'], pointsensor_channel='RADAR_FRONT')

my_scene_token = nusc.field2token("scene", "name", "scene-0061")[0]

# nusc.render_scene_channel(my_scene_token, "CAM_FRONT")
nusc.render_scene_channel(my_scene_token)

